using System;
using System.Collections.Generic;
using System.Text;
using TaugeLibrary.Classes;
using TaugeLibrary.Classes.Attributes;
using TaugeLibrary.Enums;
namespace TaugeFramework.Classes
{
    class ORM
    {
        /* return 0 = success 
         * -1=table already exists 
         * -2 not a table object
         * 1 =update column
         */
        public int CreateTable(object o)
        {
            StringBuilder _sql = new StringBuilder();

            AttTable _tbl = (AttTable)TaugeLibrary.Classes.LibConstants.getAttribute(o.GetType(), typeof(AttTable));
            if (_tbl == null)
                return -2;


            bool _isfirst = true ;
            System.Reflection.PropertyInfo[] _obj_prop = o.GetType().GetProperties();
            foreach (System.Reflection.PropertyInfo pi in _obj_prop)
            {
                AttField _fld = (AttField)TaugeLibrary.Classes.LibConstants.getAttribute(pi, typeof(AttField));
               if (_fld != null)
               {
                   if (!_isfirst)
                       _sql.Append(',');
                   _isfirst = false;

                   _sql.AppendLine(_fld.FieldName + " ");

                   switch(_fld.FieldType)
                   {
                       case eDBDataType.VARCHAR:
                           _sql.Append("VARCHAR(" + _fld.Length.ToString() + ") ");
                           break;
                       case eDBDataType.CHAR:
                           _sql.Append("CHAR(" + _fld.Length.ToString() + ") ");
                           break;
                       case eDBDataType.INT:
                       case eDBDataType.USERSTAMP:
                       case eDBDataType.USERSTAMPCREATE:
                           _sql.Append("INT ");
                           break;
                       case eDBDataType.IDENTITY:
                           _sql.Append("INT IDENTITY ");
                           break;
                       case eDBDataType.BIT:
                           _sql.Append("BIT ");
                           break;
                       case eDBDataType.MONEY:
                           _sql.Append("MONEY ");
                           break;
                       case eDBDataType.SMALLDATETIME:
                           _sql.Append("SMALLDATETIME ");
                           break;
                       case eDBDataType.DATETIME:
                       case eDBDataType.TIMESTAMP:
                       case eDBDataType.TIMESTAMPCREATE:
                           _sql.Append("DATETIME ");
                           break;                       
                   }

                   if (!_fld.Nullable)
                       _sql.Append("NOT");

                   _sql.Append(" NULL ");

               }
               
            }

            AttPrimaryKey _pk = (AttPrimaryKey)TaugeLibrary.Classes.LibConstants.getAttribute(o.GetType(), typeof(AttPrimaryKey));
            if (_pk != null)
            {
                _sql.AppendLine(" ,CONSTRAINT PK_" + _tbl.TableName + " PRIMARY KEY ( ");
                _isfirst = true;
                foreach (string s in _pk.PrimaryKeys)
                {
                    if (!_isfirst)
                        _sql.Append(",");
                    _isfirst = false;
                    _sql.Append(s);
                }
                _sql.Append(")");
            }
            
            //o.GetType().GetCustomAttributes(
            
            string _result = @"CREATE TABLE dbo." + _tbl.TableName + " ( " + _sql.ToString() + " ) " ;
            return 0;
        }

        public int Save(object o, string p_action)
        {
            return 0;
        }

        
    }
}
